package encheres.presentation.ui;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;

import encheres.manager.corba.I_CategorieHelper;
import encheres.manager.corba.I_CategorieOperations;
import encheres.manager.corba.I_ClientHelper;
import encheres.manager.corba.I_ClientOperations;
import encheres.manager.corba.I_ProduitHelper;
import encheres.manager.corba.I_ProduitOperations;

public class CorbaManager {
	
	private static ORB orb = null;
	private static I_ProduitOperations managerProduit = null;
	private static I_CategorieOperations managerCategorie = null;
	private static I_ClientOperations managerClient = null;
	private static boolean ready = false;
	
	private static void init() {
		
		try {
			// create and initialize the ORB
			Properties defaultProps = new Properties();
			FileInputStream in = new FileInputStream("src/encheres/manager/proprietesCorbaIUP.properties");
			defaultProps.load(in);
			in.close();
			orb = ORB.init((String[])null, defaultProps);
	
			// get the root naming context
			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
			NamingContext ncRef = NamingContextHelper.narrow(objRef);
			
			// resolve the Object Reference in Naming
	        NameComponent ncClient = new NameComponent("EncheresClient", "");
	        NameComponent ncCategorie = new NameComponent("EncheresCategorie", "");
	        NameComponent ncProduit = new NameComponent("EncheresProduit", "");
	        NameComponent pathClient[] = {ncClient};
	        NameComponent pathCategorie[] = {ncCategorie};
	        NameComponent pathProduit[] = {ncProduit};
	        managerClient = I_ClientHelper.narrow(ncRef.resolve(pathClient));
	        managerCategorie = I_CategorieHelper.narrow(ncRef.resolve(pathCategorie));
	        managerProduit = I_ProduitHelper.narrow(ncRef.resolve(pathProduit));
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Erreur survenue lors de l'initialisation du module Corba.");
			System.exit(0);
		} catch (InvalidName e) {
			e.printStackTrace();
			System.out.println("Erreur survenue lors de l'initialisation du module Corba.");
			System.exit(0);
		} catch (NotFound e) {
			e.printStackTrace();
			System.out.println("Erreur survenue lors de l'initialisation du module Corba.");
			System.exit(0);
		} catch (CannotProceed e) {
			e.printStackTrace();
			System.out.println("Erreur survenue lors de l'initialisation du module Corba.");
			System.exit(0);
		} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
			e.printStackTrace();
			System.out.println("Erreur survenue lors de l'initialisation du module Corba.");
			System.exit(0);
		}
		ready = true;
	}
	
	public static I_ProduitOperations getManagerProduit() {
		if (!ready) init();
		return managerProduit;
	}
	
	public static I_CategorieOperations getManagerCategorie() {
		if (!ready) init();
		return managerCategorie;
	}
	
	public static I_ClientOperations getManagerClient() {
		if (!ready) init();
		return managerClient;
	}

}
